#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget* parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 设置按钮
    okBut = new QPushButton();
    okBut->setText(QString("确认"));
    okBut->setParent(this);
    okBut->resize(100, 70);

    // 按钮样式
    okBut->setStyleSheet(QString("QPushButton:pressed { background-color: navy; }"));

    lineEdit = new QLineEdit(this); // 输入框，绑定父组件
    lineEdit->move(50, 100);
    lineEdit->resize(300, 70);
    //lineEdit->setEchoMode(QLineEdit::Password); // 密码框
    QFont font;
    font.setPixelSize(25); // 字号
    font.setBold(true); // 加粗
    lineEdit->setFont(font);

    // 文本输入框
    textEdit = new QTextEdit(this);
    textEdit->move(50, 200);
    textEdit->resize(300, 250);


    resize(800, 600);

    QIcon icon(QString(":/new/prefix1/img/myicon.png"));
    setWindowIcon(icon); // 设置窗口图标

    setWindowTitle(QString("窗口标题"));

    QPalette pal = this->palette();
    // QPalette::Background 替换为 QPalette::Window
    pal.setBrush(QPalette::Window,
                 QBrush(QPixmap(QString(":/new/prefix1/img/test1pic.png")).scaled(800, 600)));
    setPalette(pal);

    // 信号与槽
    connect(okBut, &QPushButton::clicked, this, &MainWindow::onPushButton);
    // 输入回车触发
    connect(lineEdit, &QLineEdit::editingFinished, this, &MainWindow::lineEditEnter);


    connect(this, &MainWindow::testSignal, this, &MainWindow::testSlots);

}

MainWindow::~MainWindow()
{
    delete okBut;
    delete ui;
}

void MainWindow::onPushButton()
{
    // 点击按钮，触发的动作函数
    this->textEdit->setText(textEdit->toPlainText() +  QString("hello world")); // append hello world

    emit testSlots(QString("test信号"));
}

void MainWindow::lineEditEnter()
{
    textEdit->clear();
    textEdit->setFocus(); // 设置焦点
}

void MainWindow::testSlots(QString str)
{
    lineEdit->setText(str);
}
